package com.mrx.pvzcommunity.dao;

import com.mrx.pvzcommunity.model.user.NormalUser;
import com.mrx.pvzcommunity.model.user.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 用户 相关操作
 */
@Repository
public interface UserDao {
    @Select("SELECT * FROM users")
    List<User> getAllUsers();

    @Select("SELECT * FROM users WHERE account = #{account} AND password = #{password}")
    User findUserByUser(User user);

    @Select("SELECT * FROM users WHERE account = #{account}")
    User isAccountExists(@Param("account") String account);

    @Select("SELECT * FROM users WHERE uid = #{uid}")
    User findUserByUid(@Param("uid") String uid);

    @Insert("INSERT INTO users(uid,account,password,uname,mail,problem,answer)" +
            "VALUES(#{uid},#{account},#{password},#{uname},#{mail},#{problem},#{answer})")
    Boolean addNewUser(User user);

    @Select("SELECT * FROM users WHERE uid = (SELECT fid FROM friends WHERE uid = #{uid})")
    List<NormalUser> getFriendsById(@Param("uid") String uid);

    @Insert("INSERT INTO friends VALUES(#{uid},#{fid})")
    Boolean followUserByUid(@Param("uid") String uid, @Param("fid") String fid);

    @Select("SELECT problem FROM users WHERE uid = #{uid}")
    String getProblemByUid(@Param("uid") String uid);

    @Select("SELECT * FROM users WHERE uid = #{uid} AND answer = #{answer}")
    User checkUidAndAnswer(@Param("uid") String uid, @Param("answer") String answer);
}
